#ifndef _PACKET_QUEUE_H
#define _PACKET_QUEUE_H

#include <pthread.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C"
{
#endif

#ifndef uint16_t
	typedef unsigned short uint16_t;
#endif

#ifndef uint32_t
	typedef unsigned int uint32_t;
#endif

#ifndef uint8_t
	typedef unsigned char uint8_t;
#endif

	typedef struct _Packet
	{
		char *pkt_data;	  //报文内容.
		uint16_t pkt_len; //报文长度.
		/* double linked list ptrs */
		struct _Packet *next;
		struct _Packet *prev;
	} Packet;

	typedef struct _PacketQueue
	{

		Packet *top;
		Packet *bot;
		uint32_t len;
		pthread_mutex_t lock;
	} PacketQueue;

	//#define PacketQueueLock pthread_mutex_lock
	//#define PacketQueueUnlock pthread_mutex_unlock

	Packet *PacketMalloc(uint32_t len);
	void copy_packet(Packet *p, int len, uint8_t *data);
	void pkt_free(Packet *p);

	void PacketEnqueue(PacketQueue *q, Packet *p);
	Packet *PacketDequeue(PacketQueue *q);
	bool PacketIsEmpty(PacketQueue *pq);

#define POINTER_CHECK(p)              \
	{                                 \
		if (p == NULL)                \
		{                             \
			LOG_ERROR("null point!"); \
			return NULL;              \
		}                             \
	}

#ifdef __cplusplus
}
#endif

#endif
